<?php
// +----------------------------------------------------------------------
// | HkCms 插件前台管理
// +----------------------------------------------------------------------
// | Copyright (c) 2012-2019 http://www.hkcms.cn, All rights reserved.
// +----------------------------------------------------------------------
// | Author: 广州恒企教育科技有限公司 <admin@hkcms.cn>
// +----------------------------------------------------------------------

namespace addon\message\controller;

use app\addons\util\AddonsBase;
use think\Validate;

class Index extends AddonsBase {

    /**
     * 插件首页
     * @return mixed
     */
    public function index()
    {
        $seo = seo('', '', hk_cache('Config')['siteinfo'], hk_cache('Config')['sitekeywords']);
        $this->assign("SEO", $seo);
        return $this->display();
    }

    /**
     * 插件留言
     */
    public function save()
    {
        $code = $this->request->post('code');
        if (!$this->verify($code)) {
            $this->error("验证码错误，请重新输入！");
        }

        $post = $this->request->only(['name','phone','email','content','']);
        $rule = [
            'name'  => 'require|max:25',
            'phone'   => 'number|length:11',
            'email' => 'email|max:100',
            'content' => 'require',
        ];
        $msg = [
            'name.require' => '姓名必须',
            'name.max' => '姓名长度不得超过25个字符',
            'phone.number'     => '手机格式不正确',
            'phone.length'     => '手机格式不正确',
            'email.email'   => '邮箱格式不正确',
            'email.max'   => '邮箱长度超出',
            'content.require'   => '内容必填',
        ];
        $validate = new Validate($rule,$msg);
        if (!$validate->check($post)) {
            $this->error($validate->getError());
        }

        $post['content'] = strip_tags($post['content']);
        $post['create_at'] = time();

        $bl = db('message')->insert($post);
        if ($bl) {
            $this->success('操作成功');
        } else {
            $this->error('操作失败');
        }
    }
}
